using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using jSLCore.Client.Presentation.Wpf.TelerikSupport.Controls;
using jSLCore.Utility.Extensions;
using Telerik.Windows.Controls;
#if SILVERLIGHT
using SelectionChangedEventArgs = Telerik.Windows.Controls.SelectionChangedEventArgs;
#endif
namespace jSLCore.Client.Presentation.Wpf.TelerikSupport
{
    public static class Loader
    {
        public static void InitializejBinding()
        {
            jBinding.JBindingBase.ProvideDefaultProperty += (sender, args2) =>
            {
                var element = args2.Parameter1;

                if (element is RadMaskedTextBox)
                    args2.Parameter2 = "Value";
                if(element is RadDateTimePicker)
                {
                    args2.Parameter2 = "SelectedValue";
                }

                if (element is RadDatePicker)
                {
                    args2.Parameter2 = "SelectedDate";
                }

                if(element is RadTimePicker)
                {
                    args2.Parameter2 = "SelectedTime";
                }
                if(element is RadComboBox)
                {
                    args2.Parameter2 = "SelectedValue";
                }

            };
#if SILVERLIGHT
            jBinding.JBindingBase.HandleSetAndRemoveEventHandlers += (uiElement, b, arg3) =>
                                                                         {
                                                                             if(uiElement is Selector)
                                                                             {
                                                                                 var selector = (Selector)uiElement;
                                                                                 if (b)
                                                                                 {
                                                                                     selector.SelectionChanged +=
                                                                                         selection_Changed;
                                                                                 }
                                                                                 else
                                                                                 {
                                                                                     selector.SelectionChanged -=
                                                                                         selection_Changed;
                                                                                 }
                                                                             }
                                                                         };
#endif
        }
        #if SILVERLIGHT

        private static void selection_Changed(object sender,  SelectionChangedEventArgs e)
        {
            try
            {
                var selector = (Selector)sender;

                var newValue = e.AddedItems[0].GetProperty(selector.SelectedValuePath);
                var bindings = jBinding.jValidationBinding.BindControls.Where(bc=>bc.Value==sender).Select(bc=>bc.Key).ToArray();

                foreach (var binding in bindings)
                {
                    binding.ProcessValueToData(newValue);
                }
            }
            catch (IndexOutOfRangeException)
            {

            }
        }
#endif
        public static FrameworkElement InitializeSupport(this ApplicationContainer.ApplicationContainer applicationContainer)
        {
#if SILVERLIGHT
            var source = new Uri(@"/jSLCore.Client.Presentation.Telerik;component/DefaultStyle.xaml",
                                 UriKind.RelativeOrAbsolute);
#else
            var source = new Uri(@"jSLCore.Client.Presentation.Wpf.Telerik;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
            
#endif

            var resourceDictionary = new ResourceDictionary {Source = source};
            applicationContainer.Resources.MergedDictionaries.Add(
                    resourceDictionary
                );

            var radBusyIndicator = new RadBusyIndicator {IsBusy = false,Content = applicationContainer};
            radBusyIndicator.Resources.MergedDictionaries.Add(new ResourceDictionary {Source = source});
            applicationContainer.SplashScreen = new jRadSplashScreen(radBusyIndicator);
            //return new UserControl{Content = radBusyIndicator};
            return radBusyIndicator;
        }
    }
}